﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.cms.businesslogic.web;

namespace uHelpsy.Core
{
    public static class DocumentExtensions
    {
        
        
        #region IEnumerable<Document> GetChildrenWherePropertyIs(this Document doc, string propertyName, string value)
        /// <summary>
        /// Gets children who have the property value.
        /// This method is useful for checking unpublished nodes.
        /// </summary>
        /// <param name="doc">The document</param>
        /// <param name="propertyName">Name of the property</param>
        /// <param name="value">Value to check property against</param>
        /// <returns>IEnumerable<Document></returns>
        public static IEnumerable<Document> GetChildrenWherePropertyIs(this Document doc, string propertyName, string value)
        {
            // get children which have the property with the value
            return doc.Children
                        .Where(x => x.getProperty(propertyName) != null)
                        .Where(x => (string)x.getProperty(propertyName).Value == value.ToString());
        }

        #endregion

        

        #region object GetPropertyOrDefault(this Document doc, string propertyName)
        /// <summary>
        /// Gets the value of a property, or null when it does not exist.
        /// This method is useful for checking unpublished nodes.
        /// </summary>
        /// <param name="doc">The Document</param>
        /// <param name="propertyName">The name of the property to get the value of</param>
        /// <returns>The value of the property, or null</returns>
        public static object GetPropertyOrDefault(this Document doc, string propertyName)
        {
            if (doc.getProperty(propertyName) != null)
                return doc.getProperty(propertyName).Value;
            return null;
        }

        #endregion




    }
}
